package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	router := gin.Default()
	router.GET("/query", Query)
	router.GET("/param/:userId", Param)
	router.GET("/param/:userId/:name", Param)
	router.POST("/form", PostForm)
	router.POST("/raw", RawData)
	router.Run(":8080")
}

func Query(c *gin.Context) {
	id := c.Query("id")
	name, ok := c.GetQuery("name") //如果user=""，ok为true
	if !ok {
		name = "asd"
	}
	foods, ok := c.GetQueryArray("food") // 拿多个相同的key的查询参数
	c.String(http.StatusOK, fmt.Sprintf("%s---%s---%v---%v", id, name, foods, c.DefaultQuery("userId", "123456")))
}

func Param(c *gin.Context) {
	userId := c.Param("userId")
	name := c.Param("name")
	c.String(http.StatusOK, fmt.Sprintf("%s/%s", userId, name))
}

func PostForm(c *gin.Context) {
	//c.String(http.StatusOK, fmt.Sprintf("%v", c.PostForm("name")))
	//c.String(http.StatusOK, fmt.Sprintf("%v", c.PostFormArray("name")))
	//c.String(http.StatusOK, fmt.Sprintf("%v", c.DefaultPostForm("userId", "123456")))
	form, _ := c.MultipartForm()
	c.JSON(http.StatusOK, form)
}

func RawData(c *gin.Context) {
	body, _ := c.GetRawData()
	c.String(http.StatusOK, string(body))
}
